package org.jeecg.modules.exProject.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.security.OAuthFlow;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.exChargeSetting.entity.ExChargeSetting;
import org.jeecg.modules.exChargeSetting.service.IExChargeSettingService;
import org.jeecg.modules.exOperationRecord.entity.ExOperationRecord;
import org.jeecg.modules.exOperationRecord.service.IExOperationRecordService;
import org.jeecg.modules.exProject.constant.ExProjectStepEnum;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.exProject.entity.ExTenderProject;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exProject.mapper.ExTenderProjectMapper;
import org.jeecg.modules.exProject.mapper.ExSectionMapper;
import org.jeecg.modules.exProject.mapper.ExProjectMapper;
import org.jeecg.modules.exProject.service.IExProjectService;
import org.jeecg.modules.exProject.vo.ExProjectParam;
import org.jeecg.modules.exTenderFile.entity.Chapter;
import org.jeecg.modules.exTenderFileClariModi.entity.ExTenderFileClariModi;
import org.jeecg.modules.exTenderFileClariModi.service.IExTenderFileClariModiService;
import org.jeecg.modules.exZbidOpeningArrange.entity.ExZbidOpeningArrange;
import org.jeecg.modules.exZbidOpeningArrange.service.IExZbidOpeningArrangeService;
import org.jeecg.modules.gcProject.vo.StatusEnum;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * @Description: 立项项目
 * @Author: jeecg-boot
 * @Date: 2024-08-31
 * @Version: V1.0
 */
@Service
public class ExProjectServiceImpl extends ServiceImpl<ExProjectMapper, ExProject> implements IExProjectService {

    @Autowired
    private ExProjectMapper exProjectMapper;
    @Autowired
    private ExTenderProjectMapper exTenderProjectMapper;
    @Autowired
    private ExSectionMapper exSectionMapper;
    @Autowired
    private IExOperationRecordService exOperationRecordService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(ExProject exProject, List<ExTenderProject> exTenderProjectList, List<ExSection> exSectionList) {
        exProject.setStep(ExProjectStepEnum.WAIT_AUDIT.getValue());
        exProjectMapper.insert(exProject);
        if (exTenderProjectList != null && exTenderProjectList.size() > 0) {
            for (ExTenderProject entity : exTenderProjectList) {
                //外键设置
                entity.setProjectId(exProject.getId());
                entity.setTenderProjectId(exProject.getId());
                entity.setProjectCode(exProject.getProjectCode());
                exTenderProjectMapper.insert(entity);
            }
        }
        if (exSectionList != null && exSectionList.size() > 0) {
            for (ExSection entity : exSectionList) {
                //外键设置
                entity.setProjectId(exProject.getId());
                entity.setTenderProjectId(exProject.getId());
                entity.setTenderProjectCode(exProject.getProjectCode());
                exSectionMapper.insert(entity);
            }
        }
        exProject.setProjectId(exProject.getId());
        this.updateById(exProject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(ExProject exProject, List<ExTenderProject> exTenderProjectList, List<ExSection> exSectionList) {
        exProjectMapper.updateById(exProject);

        //1.先删除子表数据
        exTenderProjectMapper.deleteByMainId(exProject.getId());
        exSectionMapper.deleteByMainId(exProject.getId());

        //2.子表数据重新插入
        if (exTenderProjectList != null && exTenderProjectList.size() > 0) {
            for (ExTenderProject entity : exTenderProjectList) {
                //外键设置
                entity.setProjectId(exProject.getId());
                entity.setTenderProjectId(exProject.getId());
                entity.setProjectCode(exProject.getProjectCode());
                exTenderProjectMapper.insert(entity);
            }
        }
        if (exSectionList != null && exSectionList.size() > 0) {
            for (ExSection entity : exSectionList) {
                //外键设置
                entity.setProjectId(exProject.getId());
                entity.setTenderProjectId(exProject.getId());
                entity.setTenderProjectCode(exProject.getProjectCode());
                exSectionMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        exTenderProjectMapper.deleteByMainId(id);
        exSectionMapper.deleteByMainId(id);
        exProjectMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            exTenderProjectMapper.deleteByMainId(id.toString());
            exSectionMapper.deleteByMainId(id.toString());
            exProjectMapper.deleteById(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean audit(ExProjectParam exProjectParam) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ExProject exProject = this.getById(exProjectParam.getId());
        if (exProject == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        exProject.setStatus(exProjectParam.getStatus());
        exProject.setUpdateTime(new Date());
        exProject.setUpdateBy(sysUser.getUsername());
        if ("2".equals(exProjectParam.getStatus())) {
            exProject.setStep(ExProjectStepEnum.XMXX.getValue());
        }
        this.updateById(exProject);
        ExOperationRecord exOperationRecord = new ExOperationRecord();
        exOperationRecord.setEntryId(exProject.getId());
        exOperationRecord.setModule("exProject");
        exOperationRecord.setOperateTime(new Date());
        exOperationRecord.setOperateResult(StatusEnum.SUCCESS.getCode().equals(exProjectParam.getStatus()) ? "1" : "2");//1 通过 2驳回
        exOperationRecord.setOperateOpinion(exProjectParam.getOperateOpinion());
        exOperationRecord.setCategory("1");
        exOperationRecord.setOperator(sysUser.getRealname());
        exOperationRecordService.save(exOperationRecord);
        return true;
    }

    @Override
    public List<ExProject> getByIds(List<String> ids) {
        LambdaQueryWrapper<ExProject> query = new LambdaQueryWrapper<ExProject>().in(ExProject::getProjectId, ids);
        return this.list(query);
    }
}
